import wx from 'wx'
import Fly from 'flyio'
const request = new Fly()

request.interceptors.request.use((request) => {
  // 给所有请求添加自定义header，带上token信息让服务器验证用户登陆
  request.headers['user-token'] = wx.getStorageSync('token')
  // wx.showLoading({ title: 'loading...' })
  wx.showNavigationBarLoading()
  // wx.showToast({ title: '加载中...', icon: 'loading' });
  return request
})

request.interceptors.response.use(
  (response, promise) => {
    // wx.hideToast()
    wx.hideNavigationBarLoading()
    // wx.hideLoading()
    return promise.resolve(response.data)
  },
  (err, promise) => {
    // wx.hideLoading()
    wx.showToast({
      title: err.message,
      icon: 'none'
    })
    wx.hideToast()
    wx.hideNavigationBarLoading()
    return promise.resolve()
  }
)

export default request
